﻿/*------------------------------------------------------
// Copyright (C) 2012 Golf  俱乐部 版权所有。 
// 文件名：  ProductDetail.aspx.cs
// 功能描述：代码后置
// 创建标识：杜风  2012-07-03
// 修改标识：
// 修改描述：
//------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Golf.WebUI
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        /// <summary>
        /// 产品编号
        /// </summary>
        public int Id = 0;
        /// <summary>
        /// 产品 bll是咧
        /// </summary>
        private Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
        /// <summary>
        /// 获取的产品实体
        /// </summary>
        private Golf.Model.Golf_Product model = new Golf.Model.Golf_Product();
        /// <summary>
        /// 产品 总的 客户评价 
        /// </summary>
        public int _star_count = 0;
        /// <summary>
        /// 当前页面的 客户评价总数 显示
        /// </summary>
        public int _count_curs = 4;
        /// <summary>
        /// 主机名称
        /// </summary>
        public string http_Host = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            http_Host = ThumNail.GetHttpHost();
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    try
                    {
                        Id = (Convert.ToInt32(Request.Params["id"]));
                        model = bll.GetModel(Id);
                        BindImg();
                        BindRerark();
                        BindPrice();
                        BindAttr();
                        Peo_Cookie();
                        CaiLike(); Bind_Star();
                    }
                    catch (Exception)
                    {
                        model = null;
                    }
                }
                else
                {
                    Response.Redirect("index.aspx");
                }
            }

        }
        /// <summary>
        /// 将当前浏览的商品保存到cookie中去
        /// </summary>
        public void Peo_Cookie()
        {
            Object obj = CookiesHelper.GetCookie("_Pro_");
            if (obj == null || obj.ToString() == "")
            {
                CookiesHelper.SaveCookie("_Pro_", Id.ToString(), 7, "D");//保存用户浏览的商品信息 7天
            }
            else
            {
                string ids = obj.ToString() + "," + Id;
                CookiesHelper.SaveCookie("_Pro_", ids, 7, "D");//保存用户浏览的商品信息 7天
            }
        }
        /// <summary>
        /// 绑定产品图片
        /// </summary>
        public void BindImg()
        {
            string[] Pro_Picture_List = null;//规格160*160
            string[] Pro_PicSmall_List = null;//规格300*300
            string[] Pro_PicBig_List = null;//规格800*800
            this.Title = model.Pro_Name + "- Online Golf";
            if (IsNotNull())
            {
                Pro_Picture_List = model.Pro_Picture.Split(',');
                Pro_PicSmall_List = model.Pro_PicSmall.Split(',');
                Pro_PicBig_List = model.Pro_PicBig.Split(',');
                _star_count = (int)model.Pro_Star;
            }
            List<Pro_Str> list = new List<Pro_Str>();
            foreach (var item in Pro_Picture_List)
            {
                Pro_Str p = new Pro_Str();
                p.Str1 = item;
                list.Add(p);
            }
            rep_proImg.DataSource = list;
            rep_proImg.DataBind();
            //做一个处理  取小图片的第一张 在加工后缀  形成大图片 _big 
            string imgSrc = Pro_Picture_List[0];
            int i = imgSrc.LastIndexOf('.');//最后的一个点"."
            string unit = imgSrc.Substring(i);//文件格式s
            imgSrc = imgSrc.Substring(0, i);
            string imgSrc_small = imgSrc + "_small" + unit;//显示的大图
            string imgSrc_big = imgSrc + "_big" + unit;//放大图
            //  绑定第一张图片 rep_proImg_001 
            List<Pro_Str> lsit001 = new List<Pro_Str>() { new Pro_Str() { Str1 = imgSrc_small, Str2 = imgSrc_big, Str3 = model.Pro_Name } };
            rep_proImg_001.DataSource = lsit001;
            rep_proImg_001.DataBind();
        }
        /// <summary>
        /// 绑定客户评价
        /// </summary>
        public void Bind_Star()
        {
            if (IsNotNull())
            {
                Golf.BLL.Golf_Comment bll = new Golf.BLL.Golf_Comment();
                List<Golf.Model.Golf_Comment> list = bll.DataTableToList(bll.GetList(4, " ProductId=" + Id, " CommentTime desc").Tables[0]);
                _count_curs = list.Count;
                rep_reviews.DataSource = list;
                rep_reviews.DataBind();
            }
        }
        /// <summary>
        /// 绑定客户评价 同时触发客户的详细资料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rep_reviews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Golf.BLL.Golf_UserData user = new Golf.BLL.Golf_UserData();
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate， 
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rep_cursdetail") as Repeater;//找到里层的repeater对象
                Golf.Model.Golf_Comment rowv = (Golf.Model.Golf_Comment)e.Item.DataItem;//找到分类Repeater关联的数据项 
                int userid = Convert.ToInt32(rowv.UserId); //获取填充子类的id  
                List<Golf.Model.Golf_UserData> list = user.DataTableToList(user.GetList(1, " UserId=" + userid, " UserId desc").Tables[0]);
                rep.DataSource = list;
                rep.DataBind();
            }
        }
        /// <summary>
        /// 猜你喜欢得产品
        /// </summary>
        public void CaiLike()
        {
            if (IsNotNull())
            {
                int typid = model.Pro_TypeId;//获得产品的类型编号
                int brandid = (int)model.Pro_BrandTypeId;//获取品牌

                Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
                // 
                List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(4, " Id<>" + Id + " and (Pro_BrandTypeId=" + brandid + " or Pro_TypeId=" + typid + ")", " Pro_Star desc").Tables[0]);
                foreach (var item in list)
                {
                    string[] temp = item.Pro_Picture.Split(',');
                    item.Pro_Picture = temp[0];
                }
                rep_like.DataSource = list;
                rep_like.DataBind();
            }

        }
        /// <summary>
        /// 绑定产品属性
        /// </summary>
        public void BindAttr()
        {
            Golf.BLL.Golf_ProAttrDetail probll = new Golf.BLL.Golf_ProAttrDetail();
            Golf.BLL.Golf_Attribute AttrBll = new Golf.BLL.Golf_Attribute();
            if (IsNotNull())
            {
                //获取产品属性
                List<Golf.Model.Golf_ProAttrDetail> prolist = probll.GetModelList("ProId=" + Id);
                string attr = null;
                string detailattr = null;

                foreach (var item in prolist)
                {
                    //父类属性
                    attr = item.AttrId;
                    //子类属性
                    detailattr = item.DetaileId;
                }

                ///父类属性
                List<Golf.Model.Golf_Attribute> AttrList = AttrBll.GetModelList("ID in(" + attr + ")");
                //子类属性
                List<Golf.Model.Golf_Attribute> DetaileList = AttrBll.GetModelList("ID in(" + detailattr + ")");

                List<Pro_Str> list = new List<Pro_Str>();
                //遍历属性 


                foreach (var item1 in AttrList)
                {
                    Pro_Str p = new Pro_Str();
                    string detailstr = "<div class='li_Two'> <select class='txt_input select_option'><option value='-1'>slelet " + item1.AttrParent + "</option>";
                    foreach (var item in DetaileList)
                    {
                        //找到父类属性了
                        if (item.ParentID == item1.ID)
                        {
                            p.Str1 = "<div class='li_One'>" + item1.AttrParent + ":</div>";
                            detailstr += "<option value='" + item.ID + "'>" + item.AttrChild + "</option>";
                        }
                    }
                    detailstr += "</select></div>";
                    p.Str2 = detailstr;
                    list.Add(p);
                }
                repAttr.DataSource = list;
                repAttr.DataBind();
            }
        }


        /// <summary>
        /// 绑定产品説明
        /// </summary>
        public void BindRerark()
        {
            if (IsNotNull())
            {
                remark.InnerHtml = model.Pro_Introduction;//产品介绍
            }
        }


        /// <summary>
        /// 绑定产品价格
        /// </summary>
        public void BindPrice()
        {
            if (IsNotNull())
            {
                //判断是否  有折扣---------没有发现字段  先默认没有折扣吧
                if (model.isCommend)//有折扣
                {
                    int save = Golf.GetSave._GetSave(model.Pro_MarketPrice, (decimal)model.Pro_PromotionPrice);
                    bind_pirce_.InnerHtml += " <p class='mrrp' style='margin-bottom: 6px; font-size: 14px; font-weight: bold; font-family: tahoma;color: #767676;'>MRRP $" + model.Pro_MarketPrice + "</p>";
                    bind_pirce_.InnerHtml += "<p class='ourPrice' style='font-size: 19px;'>OUR PRICE</p>";
                    bind_pirce_.InnerHtml += "<p class='price' style='line-height: 32px; margin-bottom: 8px; font-size: 40px; font-weight: bold; font-family: tah  oma; letter-spacing: -2px;'> $" + model.Pro_PromotionPrice + "</p>";
                    bind_pirce_.InnerHtml += " <p class='saving' style='font-size: 14px; font-weight: bold; font-family: tahoma;color: #8F8F8F'>SAVE <span>" + save + "%</span> ($" + (model.Pro_MarketPrice - (decimal)model.Pro_PromotionPrice) + ")</p> ";
                }
                else//没哟折扣
                {
                    bind_pirce_.InnerHtml += "<p class='ourPrice' style='font-size: 19px;'>OUR PRICE</p>";
                    bind_pirce_.InnerHtml += "<p class='price' style='line-height: 32px; margin-bottom: 8px; font-size: 40px; font-weight: bold; font-family: tah  oma; letter-spacing: -2px;'> $" + model.Pro_MarketPrice + "</p>";
                }
                //GolfCart/Cart.aspx?pro_Id=" + model.Id + "
                bind_pirce_.InnerHtml += " <div style='width: 146px; height: 35px; margin: 0px auto;'><a href='#' id='btnAddToBasket' style='width: 146px; height: 35px; overflow: hidden; background: url(/common/images/shell/btn_addBasket.png) no-repeat 0 0;display: block;' title='Add To Basket'></a> </div>";
            }

        }
        /// <summary>
        /// 判断 是否有产品
        /// </summary>
        /// <returns></returns>
        private bool IsNotNull()
        {

            if (model != null || model.Id != 0)
            {
                pro_Name.InnerHtml = model.Pro_Name;//产品名称
                return true;
            }
            return false;
        }

    }
}


/// <summary>
/// 没法呀  做个类在这里算了 方便点吧   此类的路径在 《Golf.WebUI.ProductDetail.aspx.cs》中 
/// </summary>
[Serializable]
public partial class Pro_Str
{
    /// <summary>
    /// 构造函数     此类的路径在 《Golf.WebUI.ProductDetail.aspx.cs》中 
    /// </summary>
    public Pro_Str() { }
    private string _Str1;
    /// <summary>
    /// 字符串1
    /// </summary>
    public string Str1
    {
        get { return _Str1; }
        set { _Str1 = value; }
    }
    private string _Str2;

    /// <summary>
    /// 字符串2
    /// </summary>
    public string Str2
    {
        get { return _Str2; }
        set { _Str2 = value; }
    }
    private string _Str3;

    /// <summary>
    ///  字符串3
    /// </summary>
    public string Str3
    {
        get { return _Str3; }
        set { _Str3 = value; }
    }
}